//
//  Int+Extension.swift
//  HuaYun
//
//  Created by enghou on 2018/11/16.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import Foundation
extension Int{
    
    //将int换成100,000这种形式
    var currencyStr: String {
        get{
            let numberFormatter = NumberFormatter()
            numberFormatter.numberStyle = .currency
            numberFormatter.locale = Locale.init(identifier: "en_CN")
            let priceNum = NSNumber.init(value: self)
            return numberFormatter.string(from: priceNum) ?? String(self)
        }
    }
    
    //将数字转换为人民币大写
    var rmbOldStyle: String {
        get{
            return String(self).rmbOldStyle
        }
    }
    
    //四舍五入运算符
    static func /(left: Int,right: Int)->Int{
        return Double(left).divide(by: Double(right))
    }
}

extension Double{
    
    func divide(by num: Double)->Int{
        return lround(self / num)
    }
    //四舍五入
    
    var currencyStr: String {
        get{
            let numberFormatter = NumberFormatter()
            numberFormatter.numberStyle = .currency
            numberFormatter.locale = Locale.init(identifier: "en_CN")
            let priceNum = NSNumber.init(value: self)
            return numberFormatter.string(from: priceNum) ?? String(self)
        }
    }
    
    var rmbOldStyle: String {
        get{
            return String(format: "%.2f", self).rmbOldStyle
        }
    }
}
